﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LikeUs.WebService.Client;
using LikeUs.WebService.Client.WSInfoClient;
using System.Drawing;
using System.Data;
using LikeUs.MovieServices;

namespace LikeUsWebSite
{
    public partial class Learning : System.Web.UI.Page
    {
        struct rate
        {
            public int movieID;
            public int Rate;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            messageLabel.Text = String.Empty;
            if (!Page.IsPostBack) //being loaded for first time
            {
                DataTable result;
                Session["nState"] = 0;
                Session["rates"]=new List<rate>();
                WSInfoProxy wsProxy = new WSInfoProxy();
                //UserData userData = wsProxy.GetMyUser((string)Session["userName"]);
                UserData userData = (UserData)Session["userData"];
                if (userData == null) return;
                int userid = (int)userData.UserID;
                result = (DataTable)wsProxy.getMoviesLearning(userid, 10);
                result.Columns.Add("name");
                MovieApiService likeusApi = new MovieApiService();
                for (int i = 0; i < result.Rows.Count; i++)
                {
                    result.Rows[i]["name"] = likeusApi.GetMovieById(Int32.Parse(result.Rows[i]["idmovies"].ToString())).Title;
                }
                Session["datasource"] = DataGrid1.DataSource;
                DataGrid1.DataSource=result;
                
                LikeUsMovie movieRes = likeusApi.GetMovieById(Int32.Parse(result.Rows[0]["idmovies"].ToString()));
                DataGrid1.DataBind();
                DataGrid1.Items[(int)Session["nState"]].ForeColor = Color.Blue;
                lbMovieName.Text = movieRes.Title + "(" + movieRes.Year + ")";
                summary.Text = movieRes.Synopsis;
                image.ImageUrl = movieRes.PosterLarge;
            }
        }

        protected void skip_Click(object sender, EventArgs e)
        {
            Response.Redirect("./Default.aspx");
        }

        protected void continue_Click(object sender, EventArgs e)
        {
            if (Request.Form["rat"] == null)
            {
                messageLabel.Text = "Please rate in order to continue";
                return;
            }
            string rating=Request.Form["rat"].ToString();
            DataTable result = (DataTable)Session["datasource"];
            List<rate> rates = (List<rate>)Session["rates"];
            rate x=new rate();
            x.Rate=Int32.Parse(rating);
            x.movieID = Int32.Parse(result.Rows[(int)Session["nState"]]["idmovies"].ToString());
            rates.Add(x);
            Session["rates"] = rates;
            DataGrid1.Items[(int)Session["nState"]].ForeColor = Color.Blue;
            Session["nState"] = (int)Session["nState"]+1;
            if ((int)Session["nState"] < DataGrid1.Items.Count)
            {
                MovieApiService likeusApi = new MovieApiService();
                LikeUsMovie movieRes = likeusApi.GetMovieById(Int32.Parse(result.Rows[(int)Session["nState"]]["idmovies"].ToString()));
                DataGrid1.Items[(int)Session["nState"]].ForeColor = Color.Blue;
                lbMovieName.Text = movieRes.Title + " ("+movieRes.Year+")";
                summary.Text = movieRes.Synopsis;
                image.ImageUrl = movieRes.PosterLarge;
            }
            else
            {
                WSInfoProxy wsProxy = new WSInfoProxy();
                //UserData userData=wsProxy.GetMyUser((string)Session["userName"]);
                UserData userData = (UserData)Session["userData"];
                int userid = (int)userData.UserID;
                foreach (rate Rate in rates){
                    wsProxy.linkUserMovie(userid, Rate.movieID, Rate.Rate);
                }
                wsProxy.userRelationsRoutine(userData);
                Response.Redirect("./Community.aspx");
            }
        }
    }
}
